{*
* 2007-2011 PrestaShop 
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2011 PrestaShop SA
*  @version  Release: $Revision: 6594 $
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*}

<!-- Block AdvancedSearch module -->
<div id="advancedsearch_block_left" class="block blockadvsearch">
    <h4>{l s='Advanced Search' mod='blockadvsearch'}</h4>

    <div class="block_content">

        

        <fieldset>
            <legend>{l s='Search by Attributes' mod='blockadvsearch'}</legend>
            <form action="">
                <p>
                    <br/>
                    {if $colors}
                    <div>{l s='Color' mod='blockadvsearch'}</div>
                    <select id="color" style="width: 75%">
                        {foreach from=$colors item=color name=color_list}
                            <option value="{$color.id_attribute}">
                                {$color.name}
                            </option>
                        {/foreach}
                    </select>
                {/if}

                <br/><br/>

                {if $attributes}
                    {foreach from=$attributes item=attribname name=attribsname_list}
                        {if $attribname.attrib|@count > 1}
                            <div class="spanattribgroup" id={$attribname.id_attrib_group}>{$attribname.name}</div>

                            <select style="width: 75%" class="selattrib" id={$attribname.name}>
                                {foreach from=$attribname.attrib item=attrib name=attrib_list}
                                    <option value="{$attrib.id_attribute}">
                                        {$attrib.name}
                                    </option>
                                {/foreach}
                            </select>
                            <br/><br/>
                        {/if}
                    {/foreach}

                {/if}

                {if $categories}


                    <div class="divcategory" id="divcategory">{l s='Category' mod='blockadvsearch'}</div>

                    <select style="width: 75%" id="advcategory">
                        {foreach from=$categories item=category name=category_list}        
                            <option value="{$category.id_category}">
                                {$category.name}
                            </option>
                        {/foreach}
                    </select>
                    <br/><br/>



                {/if}

                <a href="#" id="button_adv_search" class="exclusive" title="Search" style="margin-bottom: 5px;">{l s='Search' mod='blockadvsearch'}</a>
                </p>
            </form>     
        </fieldset>
        {literal}
            <script type="text/javascript">
                $('#button_adv_search').click(function() {
                    var data=new Array();
                    var attribgroup = new Array();
                    $( "select.selattrib" ).each( function(index) {
                        data[index] = $( this ).attr( "id" );
                    });
                    $( "div.spanattribgroup" ).each( function(index) {
                        attribgroup[index] = $( this ).attr( "id" );
                    });
                        
                        
                    
    var colorid = $('#color').val();
    var colorname = $('#color option:selected').text();
    var advsearchlink ="{/literal}{$link->getPageLink('advsearch.php')}{literal}?query_color="+colorid;
        for (var querystring in data) {
            //advsearchlink = advsearchlink +"&"+data[querystring]+"_"+attribgroup[querystring]+"="+$("#"+data[querystring]).val();
            advsearchlink = advsearchlink +"&attrib_"+attribgroup[querystring]+"="+$("#"+data[querystring]).val();
}
            advsearchlink = advsearchlink +"&advcategoryid="+$('#advcategory option:selected').val();

    $('#button_adv_search').attr('href', advsearchlink);
    //window.location.replace(advsearchlink);
                    });
            </script>
        {/literal}
        
        
        
        <!-- Block search module TOP -->
        <div id="search_block_top">
            <fieldset>
                <legend>{l s='Reference' mod='blockadvsearch'}</legend>
                <form method="get" action="{$link->getPageLink('search.php')}" id="searchbox">
                    <p>
                        <label for="search_query_top"><!-- image on background --></label>
                        <input type="hidden" name="orderby" value="position" />
                        <input type="hidden" name="orderway" value="desc" />
                        <input class="search_query" type="text" id="search_query_top" name="search_query" value="{if isset($smarty.get.search_query)}{$smarty.get.search_query|htmlentities:$ENT_QUOTES:'utf-8'|stripslashes}{/if}" />
                        <input type="submit" name="submit_search" value="{l s='Search' mod='blockadvsearch'}" class="exclusive" style="margin-top: 7px;margin-bottom: 5px;margin-left: 11%;" />
                    </p>
                </form>

            </fieldset>
        </div>
        {if $instantsearch}
            <script type="text/javascript">
            // <![CDATA[
                {literal}
                    function tryToCloseInstantSearch() {
                            if ($('#old_center_column').length > 0)
                            {
                                    $('#center_column').remove();
                                    $('#old_center_column').attr('id', 'center_column');
                                    return false;
                            }
                    }
		
                    instantSearchQueries = new Array();
                    function stopInstantSearchQueries(){
                            for(i=0;i<instantSearchQueries.length;i++) {
                                    instantSearchQueries[i].abort();
                            }
                            instantSearchQueries = new Array();
                    }
		
                    $("#search_query_top").keyup(function(){
                            if($(this).val().length > 0){
                                    stopInstantSearchQueries();
                                    instantSearchQuery = $.ajax({
                                    url: '{/literal}{if $search_ssl == 1}{$link->getPageLink('search.php', true)}{else}{$link->getPageLink('search.php')}{/if}{literal}',
                                    data: 'instantSearch=1&id_lang={/literal}{$cookie->id_lang}{literal}&q='+$(this).val(),
                                    dataType: 'html',
                                    success: function(data){
                                            if($("#search_query_top").val().length > 0)
                                            {
                                                    tryToCloseInstantSearch();
                                                    $('#center_column').attr('id', 'old_center_column');
                                                    $('#old_center_column').after('<div id="center_column">'+data+'</div>');
                                                    $("#instant_search_results a.close").click(function() {
                                                            $("#search_query_top").val('');
                                                            return tryToCloseInstantSearch();
                                                    });
                                                    return false;
                                            }
                                            else
                                                    tryToCloseInstantSearch();
                                            }
                                    });
                                    instantSearchQueries.push(instantSearchQuery);
                            }
                            else
                                    tryToCloseInstantSearch();
                    });
            // ]]>
                {/literal}
            </script>
        {/if}

        {if $ajaxsearch}
            <script type="text/javascript">
            // <![CDATA[
                {literal}
                    $('document').ready( function() {
                            $("#search_query_top")
                                    .autocomplete(
                                            '{/literal}{if $search_ssl == 1}{$link->getPageLink('search.php', true)}{else}{$link->getPageLink('search.php')}{/if}{literal}', {
                                                    minChars: 3,
                                                    max: 10,
                                                    width: 500,
                                                    selectFirst: false,
                                                    scroll: false,
                                                    dataType: "json",
                                                    formatItem: function(data, i, max, value, term) {
                                                            return value;
                                                    },
                                                    parse: function(data) {
                                                            var mytab = new Array();
                                                            for (var i = 0; i < data.length; i++)
                                                                    mytab[mytab.length] = { data: data[i], value: data[i].cname + ' > ' + data[i].pname };
                                                            return mytab;
                                                    },
                                                    extraParams: {
                                                            ajaxSearch: 1,
                                                            id_lang: {/literal}{$cookie->id_lang}{literal}
                                                    }
                                            }
                                    )
                                    .result(function(event, data, formatted) {
                                            $('#search_query_top').val(data.pname);
                                            document.location.href = data.product_link;
                                    })
                    });
                {/literal}
            // ]]>
            </script>
        {/if}
        <!-- /Block search module TOP -->

        
    </div>
</div>
<!-- /Block AdvancedSearch module -->
